package com.redlion.object;

import org.andengine.entity.scene.Scene;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.util.Vector2Pool;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.util.adt.pool.GenericPool;
import org.andengine.util.debug.Debug;
import org.andengine.util.math.MathUtils;

import android.hardware.SensorManager;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.redlion.Game;

public class CometPool extends GenericPool<Comet> {
	private static final float ORIGIN_OFFSET = 30;
	private static final double ANGULAR_FACTOR = 2;
	public ITextureRegion mTextureRegion;
	public FixtureDef mFixtureDef;
	private Scene mScene;

	public CometPool(ITextureRegion textureRegion, FixtureDef fixtureDef, Scene scene) {
		if (textureRegion == null) {
			throw new IllegalArgumentException("The texture region can not be NULL");
		}
		if (fixtureDef == null) {
			throw new IllegalArgumentException("The fixture definition can not be NULL");
		}
		mTextureRegion = textureRegion;
		mFixtureDef = fixtureDef;
		mScene = scene;
		batchAllocatePoolItems(20);
	}

	/**
	 * Called when a Bullet is required but there isn't one in the pool
	 */
	@Override
	protected Comet onAllocatePoolItem() {
		Debug.d("CometPool", "onAllocatePoolItem");
		Comet comet = new Comet(Vector2Pool.obtain(), mTextureRegion.deepCopy(), mFixtureDef);
		mScene.attachChild(comet.sprite);
		comet.deactivate();
		Game.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(comet.sprite, comet.body, true, true));
		return comet;
	}

	/**
	 * Called when a Bullet is sent to the pool
	 */
	@Override
	protected void onHandleRecycleItem(final Comet comet) {
		Debug.d("CometPool", "onHandleRecycleItem");
		// final PhysicsConnector facePhysicsConnector =
		// GameController.mPhysicsWorld.getPhysicsConnectorManager()
		// .findPhysicsConnectorByShape(comet.sprite);
		// GameController.mPhysicsWorld.unregisterPhysicsConnector(facePhysicsConnector);
		// GameController.gameActivity.getEngine().getScene().unregisterTouchArea(comet.sprite);
		// GameController.gameActivity.getEngine().getScene().detachChild(comet.sprite);
		comet.deactivate();
	}

	/**
	 * Called just before a Bullet is returned to the caller, this is where you
	 * write your initialize code i.e. set location, rotation, etc.
	 */
	@Override
	protected void onHandleObtainItem(final Comet comet) {
		Debug.d("CometPool", "onHandleObtainItem");
//		comet.activate();
		final Vector2 temp = Vector2Pool.obtain(OriginSpace.randomPosition(ORIGIN_OFFSET));
		comet.body.setTransform(temp.x, temp.y, 0);
		Vector2Pool.recycle(temp);
		comet.body.applyAngularImpulse(MathUtils.random(-1f, 1f));
		comet.body.applyLinearImpulse(MathUtils.random(-3f, 3f), MathUtils.random(-3f, 3f), comet.body.getWorldCenter().x,
				comet.body.getWorldCenter().y);
		comet.activate();
	}
}
